#include "Entity.h"
#include "MaterialManager.h"
#include "GpuProgramManager.h"

namespace DreamEngine
{
CEntity::CEntity(CRenderSystem* pRenderSystem, const CString& name):
CMovableObject(pRenderSystem, name)
{
}


CEntity::~CEntity(void)
{
}

void CEntity::OnCreate()
{
	m_pMaterial = CMaterialManager::InstancePtr()->Create("default");

	CGpuProgram* pVertexProgram = CGpuProgramManager::InstancePtr()->CreateGpuProgram(SP_VS_2_0);
	if (IsEngineErrorOk(pVertexProgram->Initialize
		("DefaultHLSL_Vertex.hlsl", "VS_MAIN")))
	{
		m_pMaterial->SetVertexProgram(pVertexProgram);
	}

	CGpuProgram* pFragmentProgram = CGpuProgramManager::InstancePtr()->CreateGpuProgram(SP_PS_2_0);
	if (IsEngineErrorOk(pFragmentProgram->Initialize
		("DefaultHLSL_Pixel.hlsl", "PS_MAIN")))
	{
		m_pMaterial->SetFragmentProgram(pFragmentProgram);
	}
	MeshLst::iterator mIter;
	for (mIter=m_meshs.begin(); mIter!=m_meshs.end(); ++mIter)
	{
		CMesh::SubMeshLst::const_iterator sbIter;
		const CMesh::SubMeshLst& subMeshs = (*mIter)->GetSubMeshs();

		for (sbIter=subMeshs.begin(); sbIter!=subMeshs.end(); ++sbIter)
		{
			(*sbIter)->GetRenderDataPtr()->m_pMaterial = m_pMaterial;
			m_renderData.push_back((*sbIter)->GetRenderDataPtr());
		}
	}
	
	m_trangleCount = CaculateTrangleCount();
}

void CEntity::OnLostDevice()
{

}

void CEntity::OnResetDevice()
{

}

void CEntity::OnDestroy()
{

}

void CEntity::AddMesh(CMesh* pMesh)
{
	m_meshs.push_back(pMesh);
}


}

